//
//  TabView.m
//  MyFramework
//
//  Created by 余洋 on 2018/10/12.
//  Copyright © 2018 余洋. All rights reserved.
//

#import "TabView.h"
#import <Masonry.h>

#define TAB_SCREEN_WIDTH ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width)

@interface TabView ()

@property (nonatomic, strong) UISegmentedControl *segmentedControl;
@property (nonatomic, strong) UIView *bottomView;//底部横线

@property (nonatomic, strong) NSArray<UIView *> *itemViews;//用来存放UISegmentedControl的子控件

@end

@implementation TabView

//通过xib加载对象后系统会调用次方法
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
//        [self createUI];
    }
    return self;
}

#pragma mark - UI
- (void)createUI {
    //设置标题
    for (int i = 0; i<self.titleArray.count; i++) {
        [self.segmentedControl insertSegmentWithTitle:self.titleArray[i] atIndex:i animated:NO];
    }
    [self addSubview:self.segmentedControl];
    [self.segmentedControl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.top.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
    self.segmentedControl.selectedSegmentIndex = 0;
    self.bottomView.backgroundColor = [UIColor redColor];
    [self addSubview:self.bottomView];
    self.itemViews = [NSArray arrayWithArray:self.segmentedControl.subviews];
    [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.itemViews[0].mas_left);
        make.height.mas_equalTo(2);
        make.bottom.mas_equalTo(self.segmentedControl.mas_bottom).mas_offset(-1);
        make.width.mas_equalTo(self.segmentedControl.mas_width).multipliedBy(1.0 / self.titleArray.count);
    }];
}

#pragma mark - Action
- (void)sementedControlClick {
    NSInteger selectIndex = self.segmentedControl.selectedSegmentIndex;
    //控制滑块
    [UIView animateWithDuration:.3 animations:^{
        self.bottomView.frame = CGRectMake(self.itemViews[selectIndex].frame.origin.x, self.bottomView.frame.origin.y, self.bottomView.frame.size.width, self.bottomView.frame.size.height);
    } completion:^(BOOL finished) {

    }];
    if (self.selectBlock) {
        self.selectBlock(selectIndex);
    }
}

- (void)setTitleArray:(NSArray *)titleArray {
    if (titleArray) {
        _titleArray = titleArray;
        [self createUI];
    }
}

#pragma mark - 懒加载
- (UISegmentedControl *)segmentedControl {
    if (!_segmentedControl) {
        _segmentedControl = [[UISegmentedControl alloc] init];
        _segmentedControl.backgroundColor = [UIColor whiteColor];
        // 设置文字样式
        [_segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]} forState:UIControlStateNormal]; //正常
        [_segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]} forState:UIControlStateHighlighted]; //按下
        [_segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected]; //选中
        
        // 设置背景图
        [_segmentedControl setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [_segmentedControl setBackgroundImage:[UIImage new] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
        
        // 设置分割线图
        [_segmentedControl setDividerImage:[UIImage new] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        
        [_segmentedControl setDividerImage:[UIImage new] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
        
        //添加监听
        [_segmentedControl addTarget:self action:@selector(sementedControlClick) forControlEvents:UIControlEventValueChanged];
    }
    return _segmentedControl;
}

- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
    }
    return _bottomView;
}
@end
